//
//  KeyboardViewController.swift
//  KeyboardExtension
//
//  Created by 周张林 on 17/8/12.
//  Copyright © 2017年 周张林. All rights reserved.
//

import UIKit

@objc
protocol KeyboardDelegate:class {
    func showNumPad() -> Void
    func showWordPad() -> Void
    @objc optional func showEmojiPad() -> Void
}

var AD_HEIGHT:CGFloat = 70

class KeyboardViewController: UIInputViewController,KeyboardDelegate {
    var wordPad:DefaultKeyboardWordPad!
    var numPad:DefaultKeyboardNumPad!
    var emojiPad:DefaultKeyboardEmojiPad!
    var bgImg:UIImageView?
    
    var heightConstraint:NSLayoutConstraint!
    var nextKeyboardButton: UIButton!
    var nextKeyboardButtonLeftSideConstraint: NSLayoutConstraint!
    
    override func updateViewConstraints()
    {
        super.updateViewConstraints()
        
        // Add custom view sizing constraints here
        if (view.frame.size.width == 0 || view.frame.size.height == 0) {
            return
        }
        
        setUpHeightConstraint()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.clear
        
        nextKeyboardButton = UIButton(type: .system)
        nextKeyboardButton.isHidden = true
        nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(nextKeyboardButton)
        
        
//        if let shareData = ShareFileManager.getGroupShareData() {
//            if let bgPath = shareData.backgroundImage , let bgData = ShareFileManager.getGroupShareFile(fileName: bgPath) {
//                bgImg = UIImageView(image:UIImage.init(data:bgData))
//                bgImg?.alpha = 0.5
//                view.addSubview(bgImg!)
//            }
//            if let bgColor = shareData.backgroundColor {
//                view.backgroundColor = UIColor.init(bgColor)
//            }
//        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // Set up constraints for next keyboard button in view did appear
        if nextKeyboardButtonLeftSideConstraint == nil {
            nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(
                item: nextKeyboardButton,
                attribute: .left,
                relatedBy: .equal,
                toItem: view,
                attribute: .left,
                multiplier: 1.0,
                constant: 0.0)
            view.addConstraints([
                nextKeyboardButtonLeftSideConstraint])
        }
        
        showWordPad()
    }
    
    // MARK: Set up height constraint
    func setUpHeightConstraint()
    {
        let customHeight:CGFloat = 216 + AD_HEIGHT
        
        if heightConstraint == nil {
            heightConstraint = NSLayoutConstraint(item: view,
                                                  attribute: .height,
                                                  relatedBy: .equal,
                                                  toItem: nil,
                                                  attribute: .notAnAttribute,
                                                  multiplier: 1,
                                                  constant: customHeight)
            heightConstraint.priority = UILayoutPriority(UILayoutPriorityRequired)
            
            view.addConstraint(heightConstraint)
        }
        else {
            heightConstraint.constant = customHeight
        }
    }
    
    
    override func viewWillLayoutSubviews() {
        resize()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        print("didReceiveMemoryWarning")
        // Dispose of any resources that can be recreated
    }
    
    func resize()->Void{
        var size = self.view.frame.size
        size.height -= AD_HEIGHT
        
        wordPad?.frame.size = size
        numPad?.frame.size = size
        emojiPad?.frame.size = size
        
        bgImg?.frame.size = view.frame.size
    }
    
    func showWordPad() -> Void {
        if wordPad == nil {
            wordPad = DefaultKeyboardWordPad()
            wordPad.inputController = self
            wordPad.delegate = self
            var frame = self.view.frame
            frame.size.height -= AD_HEIGHT
            wordPad.frame = frame
            self.view.addSubview(wordPad)
            self.requestSupplementaryLexicon(completion: { (lexicon) in
                self.wordPad.lexicon = lexicon
            })
        }
        numPad?.isHidden = true
        emojiPad?.isHidden = true
        wordPad?.isHidden = false
    }
    func showNumPad()->Void{
        if numPad == nil {
            numPad = DefaultKeyboardNumPad()
            numPad.inputController = self
            numPad.delegate = self
            var frame = self.view.frame
            frame.size.height -= AD_HEIGHT
            numPad.frame = frame
            self.view.addSubview(numPad)
        }
        numPad?.isHidden = false
        emojiPad?.isHidden = true
        wordPad?.isHidden = true
    }
    func showEmojiPad()->Void{
        if emojiPad == nil {
            emojiPad = DefaultKeyboardEmojiPad()
            emojiPad.inputController = self
            emojiPad.delegate = self
            var frame = self.view.frame
            frame.size.height -= AD_HEIGHT
            emojiPad.frame = frame
            self.view.addSubview(emojiPad)
        }
        numPad?.isHidden = true
        emojiPad?.isHidden = false
        wordPad?.isHidden = true
        
    }
    
    override func textWillChange(_ textInput: UITextInput?) {
        
    }
    
    override func textDidChange(_ textInput: UITextInput?) {
        
    }

    
    deinit {
        wordPad = nil
        numPad = nil
        emojiPad = nil
    }
}
